package com.scpii.universal.pns.dao.impl;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.impl.BaseDaoImpl;
import com.scpii.universal.pns.dao.UserActionsDao;
import com.scpii.universal.pns.entity.UserActions;

@Repository
public class UserActionsDaoImpl extends BaseDaoImpl<UserActions, Long>
		implements UserActionsDao {

	@Override
	public Integer getCountByActionType(Long targetId, String actionType) {
		String schema = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault())
				.getString("pns.schema");
		StringBuilder sql = new StringBuilder(
				MessageFormat
						.format("SELECT \"COUNT\"(1) FROM {0}.USER_ACTIONS WHERE ACTION_TYPE=:actionType and TARGET_ID=:targetId",
								schema));
		SQLQuery sqlQuery = getSession().createSQLQuery(sql.toString());
		sqlQuery.setParameter("targetId", targetId);
		sqlQuery.setParameter("actionType", actionType);
		return ((BigDecimal) sqlQuery.uniqueResult()).intValue();
	}

}